#include <string.h>

#include "mutex.h"

static void PthreadCall(const char *label, int result)
{
    if (result != 0)
    {
        fprintf(stderr, "pthread %s: %s\n", label, strerror(result));
    }
}

Mutex::Mutex()
{
    PthreadCall("init mutex", pthread_mutex_init(&mu_, NULL));
}
Mutex::~Mutex()
{
    //
    // 在 Mutex 中封装 pthread_mutex_t , 通过 ~Mutex() 对象的销毁来管理  pthread_mutex_destroy(&mu_) 销毁资源,
    // 
    PthreadCall("destroy mutex", pthread_mutex_destroy(&mu_));
}
void Mutex::Lock()
{
    PthreadCall("lock", pthread_mutex_lock(&mu_));
}
void Mutex::Unlock()
{
    PthreadCall("unlock", pthread_mutex_unlock(&mu_));
}
